﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using zhangmenren.IService;
using zhangmenren.Models;

namespace zhangmenren.Areas.Admin.Controllers
{
    [Area(areaName: "Admin")]
    public class UserController : Controller
    {

        private IUserService userService;
        private IRealService realService;
        public UserController(IUserService _userService, IRealService _realService)
       {
            userService = _userService;
            realService = _realService;
        }

        public IActionResult Index()
       {
           return View();
        }
       


        public IActionResult Add()
        {
            return View();
        }
        public async Task<string> GetUser(int limit,int page = 1,string content="")
{
            int total = 0;
            IEnumerable<UserEntity> dataList = null;
            dataList = await userService.Selelct(page, 50, content);
            if (dataList.Any())
            {
                total = dataList.Count();
            }
            return JsonConvert.SerializeObject(new { code = "0", msg = "查询成功", data = dataList, count = total });

        }

        public async Task<IActionResult> See(int id)
        {
            var entity = await realService.FindById(id);
            return View(entity);
        }

        [HttpPost]
        public async Task<JsonResult> UpState(int id,int state)
        {
            if (id!=0)
            {
                var result = await realService.UpState(id, state);
                return Json(new { state = "ok", msg = "修改成功" });
            }
            return Json(new { state = "fail", msg = "标识为空" });
        }

        [HttpPost]
        public async Task<JsonResult> IsTuiJian(int id,string date)
        {
            if (id != 0)
            {
                var result = await userService.SetTuiJian(id,Convert.ToDateTime(date));
                return Json(new { state = "ok", msg = "推荐成功" });
            }
            return Json(new { state = "fail", msg = "标识为空" });
        }

    }
}